﻿using System.Windows.Forms;
using System;
using System.Drawing;
using System.ComponentModel;

namespace xEditor.UI
{
    class FoldPanel : Panel
    {
        #region Member Variables
        
        private bool _folded;
        private bool _foldable;
        private int _orgHeight;
        private int _stringHeight;
        private Padding _padding;

        #endregion

        #region Properties
        
        public bool Folded
        {
            get { return _folded; }
            set
            {
                if (!Foldable)
                    return;

                _folded = value;

                if (_folded)
                {
                    _orgHeight = Height;
                    Height = Padding.Top;
                }
                else
                {
                    Height = _orgHeight;
                }

                Invalidate();
            }
        }
        public bool Foldable
        {
            get { return _foldable; }
            set
            {
                if (!value)
                {
                    Folded = false;
                }

                _foldable = value;
            }
        }
        public string Title { get; set; }
        public Image ImageOpened { get; set; }
        public Image ImageClosed { get; set; }
        public new Padding Padding
        {
            get { return _padding; }
            set
            {
                _padding = value;
                _padding.Top += _stringHeight + 4;
            }
        }

        #endregion

        #region Constructors

        public FoldPanel()
        {
            Foldable = true;
        }

        #endregion

        #region Functions

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            if (e.Y <= Padding.Top)
                Folded = !Folded;
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            _stringHeight = (int)e.Graphics.MeasureString(Title, Font).Height;
            if (_padding.Top < _stringHeight + 8)
                _padding.Top = _stringHeight + 8;

            base.OnPaintBackground(e);

            GraphicsUnit unit = GraphicsUnit.Display;

            int margin = 2;

            if(Foldable)
                if (_folded)
                {
                    if (ImageClosed != null)
                        e.Graphics.DrawImage(ImageClosed, new RectangleF(2 + margin, 2 + margin, _stringHeight - 2 * margin, _stringHeight - 2 * margin), ImageClosed.GetBounds(ref unit), unit);
                }
                else if (ImageOpened != null)
                    e.Graphics.DrawImage(ImageOpened, new RectangleF(2 + margin, 2 + margin, _stringHeight - 2 * margin, _stringHeight - 2 * margin), ImageOpened.GetBounds(ref unit), unit);

            e.Graphics.DrawString(Title, Font, new SolidBrush(ForeColor), (Foldable ? _stringHeight : 0) + 4 - margin, 2);
            //e.Graphics.DrawLine(new Pen(Color.Black), e.ClipRectangle.Left, e.ClipRectangle.Bottom - 1, e.ClipRectangle.Right, e.ClipRectangle.Bottom - 1);
        }

        #endregion
    }
}
